﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zhongchouwwwww.Models;

namespace Zhongchouwwwww.webform
{
    public partial class project_initiator_mesg : System.Web.UI.Page
    {
        private static Launchinfo Launchinfo;
        private static bool IsBankMsgTure = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //验证是否登录
                if (!BAL.DataVerification.IsSignin())
                    Response.Redirect("user-login.aspx");
                string ider = Request.QueryString["id"];
                string num = Request.QueryString["num"];
                if (num == null)
                {
                    if (ider == null)//验证是否由上一个页面跳转
                    {
                        Response.Redirect("project-add_item1.aspx");
                    }
                    else
                    {
                        Response.Write("<script>alert('添加项目回报信息成功')</script>");
                    }
                }
                else
                {
                    Config.Config.TempProjectID = int.Parse(num);
                    name.Enabled = true;
                    backname.Enabled = true;
                    ctiy1.Enabled = true;
                    ctiy2.Enabled = true;
                    phone.Enabled = true;
                    accountname.Enabled = true;
                    subbranch.Enabled = true;
                    banknumber.Enabled = true;
                }
            }
        }

        private static bool Insert(ZhongchouDBEntities db, Launchinfo launchinfo)
        {
                string sql = $"insert Launchinfo values('{launchinfo.launchname}','{launchinfo.address}','{launchinfo.phone}','{launchinfo.bankname}','{launchinfo.subbranch}','{launchinfo.accountname}','{launchinfo.banknumber}','{launchinfo.projectid}')";
                int result = db.Database.ExecuteSqlCommand(sql);
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }      
        }

        protected void tijiao_Click(object sender, EventArgs e)
        {
            #region 数据验证
            if (!BAL.DataVerification.IsHandset(phone.Text))
            {
                Response.Write("<script>alert('请填写正确的手机号码')</script>");
                return;
            }
            if (!BAL.DataVerification.IsBankNum(banknumber.Text))
            {
                Response.Write("<script>alert('请填写正确的银行号码')</script>");
                return;
            }
            if (!BAL.DataVerification.IsChinese(accountname.Text))
            {
                Response.Write("<script>alert('请填写正确的开户名称')</script>");
                return;
            }
            if (!BAL.DataVerification.IsChinese(subbranch.Text))
            {
                Response.Write("<script>alert('请填写正确的开户支行')</script>");
                return;
            }
            if (!BAL.DataVerification.IsChinese(backname.Text))
            {
                Response.Write("<script>alert('请填写正确的银行名称')</script>");
                return;
            }
            if (!BAL.DataVerification.IsChinese(name.Text))
            {
                Response.Write("<script>alert('请填写正确的真实姓名')</script>");
                return;
            }
            if (ctiy2.SelectedValue == "")
            {
                Response.Write("<script>alert('请选择正确的所在地址')</script>");
                return;
            }

            #endregion

            using (ZhongchouDBEntities db = new ZhongchouDBEntities())
            {
                Launchinfo = new Launchinfo
                {
                    launchname = name.Text,
                    bankname = backname.Text,
                    address = ctiy1.SelectedValue + ctiy2.SelectedValue,
                    phone = phone.Text,
                    accountname = accountname.Text,
                    subbranch = subbranch.Text,
                    banknumber = banknumber.Text,
                    projectid = Config.Config.TempProjectID.ToString()
                };
                try
                {
                    if (BAL.GetDate.db.Database.SqlQuery<int>($"select launchid from Launchinfo where  bankname = '{banknumber.Text}' and phone='{phone.Text}'").First() != 0)
                    {
                        Response.Write("<script>alert('已添加过发起人信息，修改请到个人设置处！')</script>");
                        name.Enabled = false;
                        backname.Enabled = false;
                        ctiy1.Enabled = false;
                        ctiy2.Enabled = false;
                        phone.Enabled = false;
                        accountname.Enabled = false;
                        subbranch.Enabled = false;
                        banknumber.Enabled = false;
                        return;
                    }
                }
                catch (Exception)
                {

                }
                if (Insert(db, Launchinfo) && BAL.GetDate.EXEXE($"update Projectinfo set state='待审核' where projectid='{Config.Config.TempProjectID}' and projectname='{Config.Config.projectname}'"))
                {
                    IsBankMsgTure = true;
                    Response.Write("<script>alert('添加发起人信息成功！')</script>");
                    name.Enabled = false;
                    backname.Enabled = false;
                    ctiy1.Enabled = false;
                    ctiy2.Enabled = false;
                    phone.Enabled = false;
                    accountname.Enabled = false;
                    subbranch.Enabled = false;
                    banknumber.Enabled = false;
                }
                else
                {
                    IsBankMsgTure = false;
                    Response.Write("<script>alert('添加失败')</script>");
                }
            }
        }

        protected void ctiy1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (ctiy1.SelectedValue)
            {
                case "广东省":
                    ctiy2.Items.Clear();
                    ctiy2.Items.Add("珠海市");
                    ctiy2.Items.Add("佛山市");
                    ctiy2.Items.Add("汕头市");
                    ctiy2.Items.Add("广州市");
                    ctiy2.Items.Add("深圳市");
                    break;
                case "广西省":
                    ctiy2.Items.Clear();
                    ctiy2.Items.Add("南宁市");
                    ctiy2.Items.Add("柳州市");
                    ctiy2.Items.Add("桂林市");
                    ctiy2.Items.Add("梧州市");
                    ctiy2.Items.Add("北海市");
                    break;
                case "湖北省":
                    ctiy2.Items.Clear();
                    ctiy2.Items.Add("武汉市");
                    ctiy2.Items.Add("黄石市");
                    ctiy2.Items.Add("襄阳市");
                    ctiy2.Items.Add("荆州市");
                    ctiy2.Items.Add("宜昌市");
                    break;
                default:
                    break;
            }
        }

        protected void next_step_Click(object sender, EventArgs e)
        {
            if (IsBankMsgTure)
            {
                //改为待审核状态  并且填写进度清0
                if (BAL.GetDate.db.Database.ExecuteSqlCommand($"update Projectinfo set prostate = '0', state='待审核'  where projectid='{Config.Config.TempProjectID}'") > 0)
                {
                    Response.Redirect("project-over.aspx");
                    return;
                }
                Response.Write("<script>alert('回报至少添加一项')</script>");
                return;
            }
            Response.Write("<script>alert('请添加正确的发起人信息')</script>");
        }
    }
}